package config

import "github.com/spf13/viper"

type NacosConfig struct {
	IP          string  `mapstructure:"ip"`
	Port        uint64  `mapstructure:"port"`
	Namespace   string  `mapstructure:"namespace"`
	Group       string  `mapstructure:"group"`
	ServiceName string  `mapstructure:"service_name"`
	Weight      float64 `mapstructure:"weight"`
	GrpcPort    uint64  `mapstructure:"grpc_port"`
	TimeoutMs   uint64  `mapstructure:"timeout_ms"`
	LocalIp     string  `mapstructure:"local_ip"`
}

type DataBaseConfig struct {
	Host      string `mapstructure:"host"`
	User      string `mapstructure:"user"`
	Password  string `mapstructure:"password"`
	Port      string `mapstructure:"port"`
	Dbname    string `mapstructure:"dbname"`
	Charset   string `mapstructure:"charset"`
	ParseTime bool   `mapstructure:"parse_time"`
	Local     string `mapstructure:"local"`
}

type ServerConfig struct {
	Port uint64 `mapstructure:"port"`
}

type RedisConfig struct {
	Addr     string `mapstructure:"addr"`
	Port     string `mapstructure:"port"`
	Password string `mapstructure:"password"`
	Db       int    `mapstructure:"db"`
}

type JwtConfig struct {
	Secret string `mapstructure:"secret"`
}

var ServerConf ServerConfig
var NacosConf NacosConfig
var DataBaseConf DataBaseConfig
var RedisConf RedisConfig
var JwtConf JwtConfig

func LoadConfig() {
	viper.SetConfigFile("sf-backend-user/resource/backend-user.yml")
	if err := viper.ReadInConfig(); err != nil {
		panic(err)
	}
	if err := viper.UnmarshalKey("nacos", &NacosConf); err != nil {
		panic(err)
	}
	if err := viper.UnmarshalKey("database", &DataBaseConf); err != nil {
		panic(err)
	}
	if err := viper.UnmarshalKey("server", &ServerConf); err != nil {
		panic(err)
	}
	if err := viper.UnmarshalKey("redis", &RedisConf); err != nil {
		panic(err)
	}
	if err := viper.UnmarshalKey("jwt", &JwtConf); err != nil {
		panic(err)
	}
}
